﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ScheduleManagement.UI
{
    public partial class frm_Login : Form
    {
        public frm_Login()
        {
            InitializeComponent();
        }

        //实例化登陆处理类
        ScheduleManagement.BLL.Login_Bll loginOperation = new ScheduleManagement.BLL.Login_Bll();
        //实例化用户信息操作类
        ScheduleManagement.BLL.User_BLL userOperation = new ScheduleManagement.BLL.User_BLL();
        //权限等级
        int Changepermission = 1;//1为普通用户，2为管理员，3为超级管理员

        //初始化登陆窗体
        private void frm_Login_Load(object sender, EventArgs e)
        {
            ListUserName();//列出用户名
            txtPassWord.PasswordChar = '*';
            txtDelAdminpsw.PasswordChar = '*';
            txtDelPsw.PasswordChar = '*';
            cboUserName.MaxLength = 10;//用户名最大字符数为10
            txtAddUserName.MaxLength = 10;
            txtPassWord.MaxLength = 30;//密码最大字符数为30
            txtAddPsw.MaxLength = 30;
            txtAddPswCheck.MaxLength = 30;
            pnlDelAdminCheck.Visible = false;
            pnlDelchangeAdmin.Visible = false;

            btnDelChangeOK.Enabled = false;

            cboDelChangeUser.DropDownStyle=ComboBoxStyle.DropDownList;//只能从下拉列表框中选择，不能编辑
            cboDelUser.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        //在用户下拉列表中显示最后登陆过的用户
        private void ListUserName()
        {
            //调用BLL层处理请求
            SqlDataReader dr=loginOperation.List();        
            cboUserName.Items.Clear();//添加前先把原来的数据清空
            cboDelUser.Items.Clear();
            while(dr.Read())
            {
                cboUserName.Items.Add(dr[0]);
                cboDelUser.Items.Add(dr[0]);
            }
            cboUserName.Text = cboUserName.Items[0].ToString();//用户名默认为最近登录的
        }

        //检查当前选择用户是否为记住密码
        private void cboUserName_SelectedIndexChanged(object sender, EventArgs e)
        {
            string username = cboUserName.SelectedItem.ToString();
            txtPassWord.Text = loginOperation.IsRemember(username);            
        }

        //重置
        private void btnReset_Click(object sender, EventArgs e)
        {
            cboUserName.Text = "";
            txtPassWord.Text = "";
        }

        //退出
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //登陆
        private void btnLogin_Click(object sender, EventArgs e)
        {
            ScheduleManagement.MODEL.Users.U_Name = cboUserName.Text.Trim();//用户名去除两边空格
            ScheduleManagement.MODEL.Users.U_Password = txtPassWord.Text;
            int chkstate = (int)chkRemember.CheckState;
            
            try
            {
                if (loginOperation.CheckUser(chkstate))
                {
                    //读取用户Id及权限
                    SetUserInfo();
                    this.DialogResult = DialogResult.OK;//些时登陆成功，返回父窗体 DialogResult.OK，构造主窗体frmMain
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "出错信息框", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cboUserName.Focus();//用户列表框获得焦点
            }
        }

        //读取用户Id及权限
        private void SetUserInfo()
        {
            loginOperation.SetUserInfo();
        }

        //输入用户名时进行检测
        private void cboUserName_TextChanged(object sender, EventArgs e)
        {
            string username = cboUserName.Text.ToString();
            string tempPassword = loginOperation.IsRemember(username);
            if (tempPassword.Equals(string.Empty))
                chkRemember.Checked = false;
            else
                chkRemember.Checked = true;
            txtPassWord.Text = tempPassword;
        }

        //添加用户
        private void btnAddUser_Click(object sender, EventArgs e)
        {
            try 
	        {
                string username = txtAddUserName.Text.Trim();//用户名两端不能有空格
                string psw = txtAddPsw.Text;
                string pswcheck = txtAddPswCheck.Text;
                if (userOperation.Adduser(username, psw, pswcheck))
                {
                    MessageBox.Show("添加用户成功","添加成功",MessageBoxButtons.OK);
                    tbpLogin.Show();//返回登陆标签
                    ListUserName();//刷新用户名列表
                }
	        }
	        catch (Exception ex)
	        {
        		MessageBox.Show(ex.Message, "出错信息框", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cboUserName.Focus();//用户列表框获得焦点
	        }
        }

        //删除用户
        private void btnDelUser_Click(object sender, EventArgs e)
        {
            ScheduleManagement.MODEL.Users.U_Name = cboDelUser.Text.Trim();
            ScheduleManagement.MODEL.Users.U_Password = txtDelPsw.Text;
            int i;
            try
            {
                i = userOperation.CheckUser();//获取用户权限
                if (i == 1)
                {
                    userOperation.Deluser();//普通用户删除
                    MessageBox.Show("删除用户成功", "删除成功", MessageBoxButtons.OK);
                    ListUserName();
                    txtDelPsw.Text = "";
                    cboDelUser.Text = "";
                    txtDelAdminpsw.Text = "";
                }
                else if (i == 2)
                {
                    Changepermission = 2;
                    pnlDelUser.Visible = false;
                    pnlDelAdminCheck.Visible = true;
                    lblDelAdmininfo.Text = "此用户为系统管理员，请输入管理员密码：";      
                }
                else if (i == 3)
                {
                    Changepermission = 3;
                    pnlDelUser.Visible = false;
                    pnlDelAdminCheck.Visible = true;
                    lblDelAdmininfo.Text = "此为系统超级管理员，请输入超级管理员密码：";
                }
                else
                {
                    MessageBox.Show("用户名或密码错误，请重新输入！");
                    txtDelPsw.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "出错信息框", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cboDelUser.Focus();//用户列表框获得焦点
            }
        }

        //确认管理员密码
        private void btnDelAdminCheck_Click(object sender, EventArgs e)
        {
            ScheduleManagement.MODEL.Admininfo.A_Password = txtDelAdminpsw.Text;
            ScheduleManagement.MODEL.Users.U_Name = cboDelUser.Text;
            ScheduleManagement.MODEL.Users.U_Password = txtDelPsw.Text;

            if (userOperation.CheckAdmin())
            {
                if (Changepermission == 2)
                {
                    userOperation.Deluser();
                    MessageBox.Show("删除用户成功", "删除成功", MessageBoxButtons.OK);
                    ListUserName();
                    txtDelPsw.Text = "";
                    pnlDelAdminCheck.Visible = false;
                    txtDelAdminpsw.Text = "";
                    cboDelUser.Text = "";
                    pnlDelUser.Visible = true;
                }
                else if (Changepermission == 3)
                {

                    if (cboDelUser.Items.Count > 1)
                    {
                        pnlDelchangeAdmin.Visible = true;
                        pnlDelAdminCheck.Visible = false;
                        foreach (string temp in cboDelUser.Items)
                        {
                            if (temp != ScheduleManagement.MODEL.Users.U_Name)
                            {
                                cboDelChangeUser.Items.Add(temp);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("当前系统只有一个用户，不能删除超级管理员", "删除操作失败", MessageBoxButtons.OK);
                        Changepermission = 1;
                    }
                    //userOperation.Deluser(userinfo);
                    //MessageBox.Show("删除用户成功", "删除成功", MessageBoxButtons.OK);
                    //ListUserName();
                }
            }
            else
            {
                MessageBox.Show("输入的管理密码不正确", "删除操作失败", MessageBoxButtons.OK);
            }
        }

        //取消确认管理员密码
        private void btnDelCancel_Click(object sender, EventArgs e)
        {
            pnlDelUser.Visible = true;
            pnlDelAdminCheck.Visible = false;
            Changepermission = 1;
        }

        //确认删除超级管理员
        private void btnDelChangeOK_Click(object sender, EventArgs e)
        {           
            ScheduleManagement.MODEL.Users.U_Name = cboDelUser.Text;
            string username = cboDelChangeUser.SelectedItem.ToString();
            userOperation.ChangeAdmin(username);
            userOperation.Deluser();
            MessageBox.Show("删除用户成功", "删除成功", MessageBoxButtons.OK);
            ListUserName();
            txtDelPsw.Text = "";
            cboDelUser.Text = "";
            txtDelAdminpsw.Text = "";
            pnlDelAdminCheck.Visible = false;
            pnlDelchangeAdmin.Visible = false;
            cboDelChangeUser.Text = "";
            cboDelChangeUser.Items.Clear();
            pnlDelUser.Visible = true;
        }

        //取消超级管理员权限转移
        private void btnDelChangeCancel_Click(object sender, EventArgs e)
        {
            pnlDelUser.Visible = true;
            pnlDelchangeAdmin.Visible = false;
            Changepermission = 1;
        }

        //选择用户（超级管理员权限转移）
        private void cboDelChangeUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDelChangeOK.Enabled = true;
        }
    }
}